package com.itheima.Controller;

import com.itheima.Pojo.Result;
import com.itheima.Pojo.Reward;
import com.itheima.Service.rewardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin(origins = "*", allowedHeaders = "*", methods = { RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT,
        RequestMethod.DELETE, RequestMethod.OPTIONS })
@RestController
@RequestMapping("/reward")
public class rewardController {

    @Autowired
    private rewardService rwService;

    @GetMapping
    public Result getAll() {
        return Result.success(rwService.getAll());
    }

    @PostMapping
    public Result add(@RequestBody Reward reward) {
        rwService.add(reward);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Reward reward) {
        rwService.update(reward);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) { // 使用@PathVariable接收路径参数
        log.info("删除奖励记录，ID: {}", id); // 添加日志验证
        rwService.deleteById(id);
        return Result.success();
    }

    @RequestMapping(method = RequestMethod.OPTIONS)
    public ResponseEntity<?> handleOptions() {
        return ResponseEntity.ok().build();
    }
}
